<template>
  <div id="RankingTable" v-loading="tableLoading">
    <div
      v-for="(item, index) in rankingCollegeList"
      :key="item.code"
      class="df"
      style="justify-content: space-between; margin-bottom: 20px"
    >
      <div class="df">
        <div>{{ index + 1 }}</div>
        <img class="item-img" :src="item.logoUrl" alt="" />
        <div style="width: 250px">
          <div
            class="cnName-box curr"
            style="font-size: 20px"
            @click="handleLookCollDet(item)"
          >
            {{ item.cnName }}
          </div>
          <div class="df f12" style="margin: 5px 0; flex-wrap: wrap">
            <div
              v-for="chil in item.features"
              :key="chil"
              class="chil-item"
              style="margin-bottom: 5px"
            >
              {{ chil }}
            </div>
          </div>
          <div class="f12">
            {{ item.provinceName }}&nbsp;{{ item.cityName }} /
            {{ item.natureTypeName }} / {{ item.categories[0] }} /
            {{ item.belong }}
          </div>
        </div>
        <div class="rank-box f12" style="margin-right: 100px; color: #666">
          <div>排名</div>
          <div>第{{ item.ranking }} / 综合指数{{ item.comScore }}</div>
        </div>
        <div class="heat f12" style="color: #666">
          <div>热度</div>
          <div>{{ (item.hits / 10000).toFixed(1) }}万</div>
        </div>
      </div>

      <div class="my-btn f14 curpoin" @click="handleComparison(item)">
        <div
          :class="{ isActive: item.isSelect }"
          class="df"
          style="justify-content: center"
        >
          <el-icon size="12" v-if="!item.isSelect" style="margin-right: 8px"
            ><Plus
          /></el-icon>
          {{ item.isSelect ? '已加入' : '对比' }}
        </div>
      </div>
    </div>
  </div>
  <loginCard ref="loginCardRef" />
</template>

<script setup>
import { ref, computed, watch } from 'vue';
import { getToken } from '@/utils/auth';
const loginCardRef = ref('');
import { useStore } from 'vuex';
import loginCard from '@/components/LoginCard';

import bus from '../../../../utils/bus';
import { getLikeSave } from '@/api/searchColleges/searchCollege';
import { useRouter } from 'vue-router';
const router = useRouter();
const { rankingCollegeList } = defineProps({
  rankingCollegeList: {
    type: Array,
    default: () => [],
  },
});
const store = useStore();
const checkedDataList = ref([]);
const saveContrast = ref({
  type: 'compare',
  targetid: '',
});
const tableLoading = ref(false);
const selectedCollegeData = computed(
  () => store.getters.searchCollege.selectedCollege,
);
watch(selectedCollegeData, (newVal) => {
  checkedDataList.value = newVal;
});
const fetchLikeSave = () => {
  getLikeSave(saveContrast.value).then((res) => {
    if (res.data.code == 20000) {
      store.dispatch('searchCollege/setCollegeLike', checkedDataList.value);
    }
  });
};
const handleComparison = (data) => {
  if (!getToken()) {
    loginCardRef.value.handleVisibleShow();
  } else {
    data.isSelect = !data.isSelect;
    if (data.isSelect) {
      bus.emit('changeIsShow', true);
      saveContrast.value.targetid = data.code;
      checkedDataList.value.push(data);
      checkedDataList.value = [...new Set(checkedDataList.value)];
      fetchLikeSave();
    } else {
      saveContrast.value.targetid = data.code;
      saveContrast.value.operate = 'del';
      const index = checkedDataList.value.findIndex(
        (item) => item.code === data.code,
      );
      // 如果在对比列表中找到该项，则移除
      if (index !== -1) {
        checkedDataList.value.splice(index, 1); // 删除元素
        fetchLikeSave(); // 发送请求
      }
    }
  }
};
const handleLookCollDet = (item) => {
  localStorage.setItem('collegeDetAct', '1');
  localStorage.setItem('code', item.code);
  window.open(
    router.resolve('/college/detail?collegecode=' + item.code).href,
    '_blank',
  );
};
const handleRankingIsTableLoading = (isLoading) => {
  tableLoading.value = isLoading;
};
defineExpose({
  handleRankingIsTableLoading,
});
</script>

<style lang="scss" scoped>
#RankingTable {
  width: 70%;
  .item-img {
    width: 72px;
    margin: 15px 15px;
  }
  .chil-item {
    margin-right: 5px;
    padding: 2px 5px;
    border: 1px solid #d3d4d6;
    border-radius: 4px;
    color: #909399;
  }
  .cnName-box:hover {
    color: var(--color-text);
  }
  .my-btn {
    width: 80px;
    border-radius: 4px;
    justify-content: center;
    color: var(--color-text);
    border: 1px solid var(--color-text);

    &:hover {
      background: var(--color-text);
      color: #fff;
    }
  }
}
.isActive {
  background: var(--color-text);
  color: #fff;
}
</style>
